home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / WIN32EVTLOGUTIL.PYC (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  1999-07-21  |  5.9 KB  |  120 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 1.5)
  3.  
  4. '''Event Log Utilities - helper for win32evtlog.pyd
  5. '''
  6. import win32api
  7. import win32con
  8. import winerror
  9. import win32evtlog
  10. import string
  11. error = win32api.error
  12. langid = win32api.MAKELANGID(win32con.LANG_ENGLISH, win32con.SUBLANG_ENGLISH_US)
  13.  
  14. def AddSourceToRegistry(appName, msgDLL, eventLogType = 'Application'):
  15.     '''Add a source of messages to the event log.
  16.  
  17.   Allows Python program to register a custom source of messages in the
  18.   registry.  You must also provide the DLL name that has the message table, so the
  19.   full message text appears in the event log.
  20.  
  21.   Note that the win32evtlog.pyd file has a number of string entries with just "%1"
  22.   built in, so many Python programs can simply use this DLL.  Disadvantages are that
  23.   you do not get language translation, and the full text is stored in the event log,
  24.   blowing the size of the log up.
  25.   '''
  26.     hkey = win32api.RegCreateKey(win32con.HKEY_LOCAL_MACHINE, 'SYSTEM\\CurrentControlSet\\Services\\EventLog\\%s\\%s' % (eventLogType, appName))
  27.     win32api.RegSetValueEx(hkey, 'EventMessageFile', 0, win32con.REG_EXPAND_SZ, msgDLL)
  28.     data = win32evtlog.EVENTLOG_ERROR_TYPE | win32evtlog.EVENTLOG_WARNING_TYPE | win32evtlog.EVENTLOG_INFORMATION_TYPE
  29.     win32api.RegSetValueEx(hkey, 'TypesSupported', 0, win32con.REG_DWORD, data)
  30.     win32api.RegCloseKey(hkey)
  31.  
  32.  
  33. def RemoveSourceFromRegistry(appName, eventLogType = 'Application'):
  34.     '''Removes a source of messages from the event log.
  35.   '''
  36.     
  37.     try:
  38.         win32api.RegDeleteKey(win32con.HKEY_LOCAL_MACHINE, 'SYSTEM\\CurrentControlSet\\Services\\EventLog\\%s\\%s' % (eventLogType, appName))
  39.     except win32api.error:
  40.         (hr, fn, desc) = None
  41.         if hr != ERROR_FILE_NOT_FOUND:
  42.             raise 
  43.         
  44.     except:
  45.         hr != ERROR_FILE_NOT_FOUND
  46.  
  47.  
  48.  
  49. def ReportEvent(appName, eventID, eventCategory = 0, eventType = win32evtlog.EVENTLOG_ERROR_TYPE, strings = None, data = None, sid = None):
  50.     '''Report an event for a previously added event source.
  51.   '''
  52.     hAppLog = win32evtlog.RegisterEventSource(None, appName)
  53.     win32evtlog.ReportEvent(hAppLog, eventType, eventCategory, eventID, sid, strings, data)
  54.     win32evtlog.DeregisterEventSource(hAppLog)
  55.  
  56.  
  57. def FormatMessage(eventLogRecord, logType = 'Application'):
  58.     '''Given a tuple from ReadEventLog, and optionally where the event
  59. \trecord came from, load the message, and process message inserts.
  60.  
  61. \tNote that this function may raise win32api.error.  See also the
  62. \tfunction SafeFormatMessage which will return None if the message can
  63. \tnot be processed.
  64. \t'''
  65.     keyName = 'SYSTEM\\CurrentControlSet\\Services\\EventLog\\%s\\%s' % (logType, eventLogRecord.SourceName)
  66.     handle = win32api.RegOpenKey(win32con.HKEY_LOCAL_MACHINE, keyName)
  67.     
  68.     try:
  69.         dllName = win32api.RegQueryValueEx(handle, 'EventMessageFile')[0]
  70.         dllName = win32api.ExpandEnvironmentStrings(dllName)
  71.         dllHandle = win32api.LoadLibraryEx(dllName, 0, win32con.DONT_RESOLVE_DLL_REFERENCES)
  72.         
  73.         try:
  74.             data = win32api.FormatMessageW(win32con.FORMAT_MESSAGE_FROM_HMODULE, dllHandle, eventLogRecord.EventID, langid, eventLogRecord.StringInserts)
  75.         finally:
  76.             win32api.FreeLibrary(dllHandle)
  77.  
  78.     finally:
  79.         win32api.RegCloseKey(handle)
  80.  
  81.     return data
  82.  
  83.  
  84. def SafeFormatMessage(eventLogRecord, logType = None):
  85.     '''As for FormatMessage, except returns an error message if
  86. \tthe message can not be processed.
  87. \t'''
  88.     if logType is None:
  89.         logType = 'Application'
  90.     
  91.     
  92.     try:
  93.         return FormatMessage(eventLogRecord, logType)
  94.     except win32api.error:
  95.         if eventLogRecord.StringInserts is None:
  96.             desc = ''
  97.         else:
  98.             desc = string.join(map((lambda x: str(x)), eventLogRecord.StringInserts), ', ')
  99.         return '<The description for Event ID ( %d ) in Source ( %s ) could not be found. It contains the following insertion string(s):%s.>' % (winerror.HRESULT_CODE(eventLogRecord.EventID), eventLogRecord.SourceName, desc)
  100.  
  101.  
  102.  
  103. def FeedEventLogRecords(feeder, machineName = None, logName = 'Application', readFlags = None):
  104.     if readFlags is None:
  105.         readFlags = win32evtlog.EVENTLOG_BACKWARDS_READ | win32evtlog.EVENTLOG_SEQUENTIAL_READ
  106.     
  107.     h = win32evtlog.OpenEventLog(machineName, logName)
  108.     
  109.     try:
  110.         while 1:
  111.             objects = win32evtlog.ReadEventLog(h, readFlags, 0)
  112.             if not objects:
  113.                 break
  114.             
  115.             map((lambda item, feeder = feeder: apply(feeder, (item,))), objects)
  116.     finally:
  117.         win32evtlog.CloseEventLog(h)
  118.  
  119.  
  120.